namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System.Xml.Serialization;
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class OptionGroupFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<OptionGroup>()
                .DerivesFrom<Element>()
                .IsConcreteClass()
                .IsUnsealed()
                .HasDefaultConstructor()
                .Implements<IOption>()
                .Implements<IAttributeDisabled>()
                .Implements<IXmlSerializable>()
                .XmlRoot("optgroup")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            OptionGroup expected = new OptionGroup
            {
                Id = "id",
                Class = "class",
                Disabled = true,
                Label = "label",
                Language = "fr"
            };

            string xml = "<optgroup id='id' class='class' lang='fr' label='label' disabled='disabled'><foo /></optgroup>";
            OptionGroup actual = xml.Deserialize<OptionGroup>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void deserializeEmpty()
        {
            Assert.NotNull("<optgroup />".Deserialize<OptionGroup>());
        }

        [Fact]
        public void serialize()
        {
            OptionGroup obj = new OptionGroup
            {
                Id = "id",
                Class = "class",
                Disabled = true,
                Label = "label",
                Language = "fr",
                Title = "title"
            };

            string xpath = "1=count(/optgroup[@id='id'][@class='class'][@lang='fr'][@title='title'][@label='label'][@disabled='disabled'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new OptionGroup());
        }

        [Fact]
        public void prop_Disabled()
        {
            Assert.True(new PropertyExpectations(typeof(OptionGroup).GetProperty("Disabled"))
                .IsAutoProperty<bool>()
                .Result);
        }

        [Fact]
        public void prop_Label()
        {
            Assert.True(new PropertyExpectations(typeof(OptionGroup).GetProperty("Label"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Options()
        {
            Assert.True(new PropertyExpectations(typeof(OptionGroup).GetProperty("Options"))
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_Title()
        {
            Assert.True(new PropertyExpectations(typeof(OptionGroup).GetProperty("Title"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void op_Deserialize_string_string_string()
        {
            OptionGroup expected = new OptionGroup
            {
                Id = "id",
                Class = "class",
                Disabled = true,
                Label = "label",
                Language = "fr",
                Title = "title",
                Options =
                {
                    new Option { Text = "text1", Value = "value1" },
                    new Option { Text = "text2", Value = "value2" }
                }
            };

            string xml =
                "<optgroup id='id' class='class' lang='fr' title='title' label='label' disabled='disabled'>" +
                    "<option value='value1'>text1</option>" +
                    "<option value='value2'>text2</option>" +
                "</optgroup>";
            OptionGroup actual = new OptionGroup().Deserialize("optgroup", null, xml) as OptionGroup;

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderCollection()
        {
            OptionGroup expected = new OptionGroup
            {
                Id = "id",
                Class = "class",
                Disabled = true,
                Label = "label",
                Language = "fr",
                Title = "title",
                Options =
                {
                    new Option { Text = "text1", Value = "value1" },
                    new Option { Text = "text2", Value = "value2" }
                }
            };

            string xml =
                "<optgroup id='id' class='class' lang='fr' title='title' label='label' disabled='disabled'>" +
                    "<option value='value1'>text1</option>" +
                    "<option value='value2'>text2</option>" +
                "</optgroup>";
            OptionGroup actual = xml.Deserialize<OptionGroup>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void IXmlSerializable_op_WriteXml_XmlWriterCollection()
        {
            OptionGroup obj = new OptionGroup
            {
                Id = "parent",
                Options =
                {
                    new Option { Id = "child1" },
                    new Option { Id = "child2" }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();
            Assert.True((bool)navigator.Evaluate("1=count(/optgroup[@id='parent']/option[@id='child1'])"));
            Assert.True((bool)navigator.Evaluate("1=count(/optgroup[@id='parent']/option[@id='child2'])"));
        }
    }
}